<layout name='Layout/layout_main' />
<script type="text/javascript">
	function save() {
		$("#node_list").val('');
		$(".wrap .tree_menu input[name='node_id']:checked").each(function() {
			$("#node_list").val($("#node_list").val() + $(this).val() + ",");
		})
		sendForm("form_data", "{:U('set_node')}");
	};

	function showdata(result) {
		$(".wrap .tree_menu input").each(function() {
			$(this).iCheck('uncheck');
			$(this).parents("td").removeClass("active");
		});
		for (var s in result.data) {
			$(".wrap .tree_menu input[name='node_id'][value=" + result.data[s].node_id + "]").iCheck('check');

			if (result.data[s].admin == 1) {
				$(".wrap .tree_menu input[name='admin[]'][value=" + result.data[s].node_id + "]").iCheck('check');
				$(".wrap .tree_menu input[name='admin[]'][value=" + result.data[s].node_id + "]").parents("td").addClass("active");
			}
			if (result.data[s].write == 1) {
				$(".wrap .tree_menu input[name='write[]'][value=" + result.data[s].node_id + "]").iCheck('check');
				$(".wrap .tree_menu input[name='write[]'][value=" + result.data[s].node_id + "]").parents("td").addClass("active");
			}
			if (result.data[s].read == 1) {
				$(".wrap .tree_menu input[name='read[]'][value=" + result.data[s].node_id + "]").iCheck('check');
				$(".wrap .tree_menu input[name='read[]'][value=" + result.data[s].node_id + "]").parents("td").addClass("active");
			}
			$(".wrap .tree_menu input[node=" + result.data[s].node_id + "]").parents("td").addClass("active");
		};
		$("#org_node_list").val('');
		$(".wrap .tree_menu input[name='node_id']:checked").each(function() {;
			$("#org_node_list").val($("#org_node_list").val() + $(this).val() + ",");
		})
		$("#opmode").val("edit");
	};

	$(document).ready(function() {
		set_return_url();
		$(".role_list tbody tr").click(function() {
			$(".role_list  tr").attr("class", "");
			$(this).attr("class", "active");
			$("#role_id").val($(this).attr("id"));
			sendAjax("{:U('get_node_list')}", "role_id=" + $(this).attr("id"), function(data) {
				showdata(data);
			});
			return false;
		})

		$(".wrap .tree_menu input").on('ifChecked', function(event) {
			$(this).parents("td").addClass("active");
			event.stopPropagation();
		})

		$(".wrap .tree_menu input").on('ifUnchecked', function(event) {
			$(this).parents("td").removeClass("active");
			event.stopPropagation();
		})

		$(".wrap .tree_menu  td").click(function() {
			if ($(this).find("input").attr("checked") == "checked") {
				$(this).find("input").iCheck('uncheck');
				$(this).removeClass("active");
			} else {
				if ($(this).find("input").length > 0) {
					$(this).find("input").iCheck('check');
					$(this).addClass("active");
				}
			}
		})
	}); 
</script>
<div class="title panel panel-default">
	<div class="panel-body">
		<h3 class="col-md-12">权限组管理</h3>
	</div>
</div>
<form id="form_search" name="form_search" method="post" action="">
	<div class="operate panel panel-default">
		<div class="panel-body">
			<div class="pull-left">
				<select name="s_pid" id="s_pid" class="form-control col-10" onchange="search();">					
					{:fill_option($groupList)}
				</select>				
			</div>
			<div class="pull-right">
				<a onclick="save()" class="btn btn-default">保存</a>
			</div>
		</div>
	</div>
</form>
<div class="row">
	<div class="col-md-4 role_list">
		<table class="table table-bordered">
			<thead>
				<tr>
					<th>名称</th>
				</tr>
			</thead>
			<tbody>
				<volist name="list" id="data">
					<tr id="{$data.id}">
						<td>{$data.name}</td>
					</tr>
				</volist>
			</tbody>
		</table>
	</div>
	<div class="col-md-8 wrap">
		<form id="form_data" method="post" action="">
			<input type="hidden" name="ajax" id="ajax" value="1">
			<input type="hidden" name="role_id" id="role_id">
			<input type="hidden" name="org_node_list" id="org_node_list">
			<input type="hidden" name="node_list" id="node_list">
			<div class="wrap tree_menu">
				<table class="table table-bordered">
					<tr>
						<th style="width:55%;text-align:center">菜单</th>
						<th style="width:15%;text-align:center">读取</th>
						<th style="width:15%;text-align:center">修改</th>
						<th style="width:15%;text-align:center">管理</th>
					</tr>
					<volist name="node_list" id="data">
						<tr >
							<td><i class="icon level{$data['level']+1}"></i><span class="checkbox">
								<input node="{$data.id}" type="checkbox" name="node_id" value="{$data.id}">
							</span> {$data.name}</td>
							<if condition="strpos($data['url'],'/index')">
								<td style="text-align:center"><span class="checkbox">
									<input type="checkbox" name="read[]" value="{$data.id}">
								</span></td>
								<td  style="text-align:center"><span class="checkbox">
									<input type="checkbox" name="write[]" value="{$data.id}">
								</span></td>
								<td  style="text-align:center"><span class="checkbox">
									<input type="checkbox" name="admin[]" value="{$data.id}">
								</span></td>
								<else/>
								<td class="check_box text-center">&nbsp;</td>
								<td class="check_box text-center">&nbsp;</td>
								<td class="check_box text-center">&nbsp;</td>
							</if>
						</tr>
					</volist>
				</table>
			</div>
		</form>
	</div>
</div>